#!/bin/bash

# File = hole6.cnf
# Blocks = 2
# Solver configuration:
# Input file: hole6.cnf
# Formula has 42 variables and 133 clauses
# Parallelization strategy: Divide and Conquer
# Number of blocks: 2
# Number of jobs = 32
# UNSAT
# 14.51user 2.71system 0:17.30elapsed 99%CPU (0avgtext+0avgdata 97856maxresident)k
# 0inputs+0outputs (0major+3406minor)pagefaults 0swaps

FILE=$1
BC_SCALE=5

NUMBER_OF_BLOCKS=`cat $FILE | egrep -x "Blocks = [0-9]+" | sort | uniq | wc -l`
NUMBER_OF_THREADS=`cat $FILE | egrep -x "Threads = [0-9]+" | sort | uniq | wc -l`
NUMBER_OF_FILES=`cat $FILE | egrep -x "File = .+" | sort | uniq | wc -l`
NUMBER_OF_JOBS=`cat $FILE | egrep -x "Number of jobs = [0-9]+" | sort | uniq | wc -l`

BLOCKS_COUNT=0
FILES_COUNT=0

CURRENT_FILE_INDEX=none
CURRENT_BLOCK_INDEX=none

let LAST_BLOCK=$NUMBER_OF_BLOCKS-1
let LAST_FILE=$NUMBER_OF_FILES-1
let LAST_JOB=$NUMBER_OF_JOBS

for JOB in $(seq 0 $LAST_JOB)
do
	for THE_FILE in $(seq 0 $LAST_FILE)
	do
        	for BLOCK in $(seq 0 $LAST_BLOCK)
	        do
        	        let INDEX=$THE_FILE*$NUMBER_OF_BLOCKS+$BLOCK+$JOB*$NUMBER_OF_BLOCKS*$NUMBER_OF_FILES
                	COUNT[$INDEX]=0
	                SUM[$INDEX]=0
        	done
	done
done

JOBS[0]="1"
JOBS_COUNT=1
CURRENT_JOB_INDEX=0

CURRENT_JOB=none
DEFAULT_JOB=1

function contains() {
    local n=$#
    local value=${!n}
    for ((i=0;i < $#;i++)) {
        if [ "${!i}" == "${value}" ]; then
            echo "y"
                let i--
            return $i
        fi
    }
    echo "n"
    return 0
}



while read line
do

	if [[ "$line" =~ ^"Blocks = "[0-9]+$ ]]
        then
                BLOCK=`echo $line | cut -d " " -f 3`

                CONTAINS=$(contains "${BLOCKS[@]}" "$BLOCK")
                INDEX=$?

                if [[ $CONTAINS == "n" ]]
                then
                        BLOCKS[$BLOCKS_COUNT]=$BLOCK
                        CURRENT_BLOCK_INDEX=$BLOCKS_COUNT
                        let BLOCKS_COUNT++
                else

                        CURRENT_BLOCK_INDEX=$INDEX

                fi

        fi

        if [[ "$line" =~ ^"File = ".+$ ]]
        then
                FILE=`echo $line | cut -d " " -f 3`

                CONTAINS=$(contains "${FILES[@]}" "$FILE")
                INDEX=$?

                if [[ $CONTAINS == "n" ]]
                then
                        FILES[$FILES_COUNT]=$FILE
                        CURRENT_FILE_INDEX=$FILES_COUNT
                        let FILES_COUNT++
                else

                        CURRENT_FILE_INDEX=$INDEX

                fi


        fi

	if [[ "$line" =~ ^"Formula has "[0-9]+" variables and "[0-9]+" clauses"$ ]]
	then
		VAR=`echo $line | cut -d " " -f 3`
		CLA=`echo $line | cut -d " " -f 6`

		VARIABLES[$CURRENT_FILE_INDEX]=$VAR
		CLAUSES[$CURRENT_FILE_INDEX]=$CLA

	fi

	if [[ "$line" == "SAT" ]] || [[ "$line" == "UNSAT" ]] || [[ "$line" == "UNDEF" ]]
	then
		SAT_STATUS[$CURRENT_FILE_INDEX]=$line
	fi

	if [[ "$line" =~ ^"Number of jobs = "[0-9]+$ ]]
	then
		CURRENT_JOB=`echo $line | cut -d " " -f 5`

                CONTAINS=$(contains "${JOBS[@]}" "$CURRENT_JOB")
                INDEX=$?

                if [[ $CONTAINS == "n" ]]
                then
                        JOBS[$JOBS_COUNT]=$CURRENT_JOB
                        CURRENT_JOB_INDEX=$JOBS_COUNT
                        let JOBS_COUNT++
                else

                        CURRENT_JOB_INDEX=$INDEX

                fi


	fi

	if [[ "$line" =~  ^[0-9]+("."[0-9]*)?"user "[0-9]+("."[0-9]*)?"system "([0-9]+":")?[0-9]+":"[0-9]+("."[0-9]*)?"elapsed" ]]
	then

		if [[ "$CURRENT_JOB" == "none" ]]
		then
			CURRENT_JOB=$DEFAULT_JOB
			CURRENT_JOB_INDEX=0
		fi

		CURRENT_VALUE=$line

		if [[ "$line" =~  ^[0-9]+("."[0-9]*)?"user "[0-9]+("."[0-9]*)?"system "[0-9]+":"[0-9]+":"[0-9]+("."[0-9]*)?"elapsed" ]]
		then
			CURRENT_VALUE=`echo "$line" | cut -d " " -f 3 | cut -d "e" -f 1 | sed s/:/*3600+/1`

		fi

		CURRENT_VALUE=`echo "$CURRENT_VALUE" | cut -d " " -f 3 | cut -d "e" -f 1 | sed s/:/*60+/g`
		CURRENT_VALUE=`echo "scale = $BC_SCALE; $CURRENT_VALUE" | bc`

		let INDEX=${CURRENT_FILE_INDEX}*${NUMBER_OF_BLOCKS}+${CURRENT_BLOCK_INDEX}
		let AUX=${NUMBER_OF_BLOCKS}*${NUMBER_OF_FILES}
		let INDEX=${CURRENT_JOB_INDEX}*${AUX}+${INDEX}

		let COUNT[$INDEX]++
		SUM[$INDEX]=`echo "scale = ${BC_SCALE}; $CURRENT_VALUE + ${SUM[$INDEX]}" | bc`

		CURRENT_JOB=none

	fi

done < $FILE

for JOB in $(seq 0 $LAST_JOB)
do

	for THE_FILE in $(seq 0 $LAST_FILE)
	do
        	for BLOCK in $(seq 0 $LAST_BLOCK)
        	do
                	let INDEX=$THE_FILE*$NUMBER_OF_BLOCKS+$BLOCK+$JOB*$NUMBER_OF_BLOCKS*$NUMBER_OF_FILES
	                if [ "${COUNT[$INDEX]}" -ne "0" ]
        	        then
			        echo "File = ${FILES[$THE_FILE]}"
				echo "Variables = ${VARIABLES[$THE_FILE]}"
				echo "Clauses = ${CLAUSES[$THE_FILE]}"
				echo "Status = ${SAT_STATUS[$THE_FILE]}"
	                	echo "Block = ${BLOCKS[$BLOCK]}"
				echo "Jobs = ${JOBS[$JOB]}"
	                        AVERAGE=`echo "scale = $BC_SCALE; ${SUM[$INDEX]}/${COUNT[$INDEX]}" | bc`
        	                echo -e "Average = "$AVERAGE
        		        echo -e "Count = "${COUNT[$INDEX]}

	                fi
		done
	done

done
